import java.util.HashMap;
import java.util.Map;

/**
 * @author mifei
 * @create 2018年04月18日
 **/
public class Helper {

    public ConsumeBean consumeBean;
    public String input;
    public String consumeCounponExtList;
    public String consumeCouponRelItemList;
    public String calcDonateMoney;
    /**
     * 消费返现金额
     */
    public String consumeDonateMoney;
    public String posConsumeRule;  //消费规则

    /**
     * 参与赠送积分金额
     */
    public String calcScoreMoney;
    /**
     * 赠送积分
     */
    public String donatedScore;

    /**
     * 消费返现账户
     */
    private String consumeDonateAccount;
    /**
     * 本金账户
     */
    private String realAccount;
    /**
     * 赠送账户
     */
    private String freeAccount;
    /**
     * 积分账户
     */
    private String scoreAccount;
    /**
     * 消费设置
     */
    private String consumeSet = null;
    /**
     * 卡账户流水记录集合
     */
    public String consumeBean_accountRecords;
    public String consumeBean_cardAccountsChange;
    /**
     * 返现产生记录List(半成品)  此次消费要更新的返现产生记录
     */
    public String consumeBean_updateDonateGenerateRecordList;
    // sql 表名
    String bm_card_account_consumption_ext = "消费的扩展表，里面有具体的业务";
    String bm_card_account = "";
    String bm_consume_rel_company_cfg = "";
    String bm_consume_donate_generate_record = "";
    String bm_card_account_record = "";
    String bm_consume_donate_cost_record = "";
    String bm_consume_coupon_rel_item = "";
    String bm_card_account_consumption_coupon_ext = "";


    //
    public String posConsumeRelCompanyCfgMapper_selectByCompanyId = "posConsumeRelCompanyCfgMapper.selectByCompanyId";
    public String posConsumeDonateGenerateRecMapper_insert = bm_consume_donate_generate_record;
    private String posConsumeDonateGenerateRecMapper_listAbleUseRec;
    private String posConsumeRuleMapper_selectByParams;
    private String posConsumeRuleMapper_selectRelItemsByRuleId;
    private String posConsumeDonateGenerateRecMapper_updateByChange = bm_consume_donate_generate_record;
    private String posConsumeCouponRelItemMapper_batchInsert = bm_consume_coupon_rel_item;
    private String posConsumptionCouponExtMapper_batchInsert = bm_card_account_consumption_coupon_ext;
    private String posConsumeDonateCostRecMapper_insert = bm_consume_donate_cost_record;
    private String cardAccountMapper_updateRecordChange = bm_card_account;
    private String cardAccConsumeExtMapper_insertSelective = bm_card_account_consumption_ext;
    private String cardAccountRecordMapper_insertSelective = bm_card_account_record;

    private String consumeBean_consumeDonateCostRecordList;
    /**
     * 返现产生记录List(半成品)  此次消费产生的
     */
    public String consumeBean_insertDonateGenerateRecordList;
    Map map = new HashMap<>();
    private String posConsumeRule_relItems;

    /**
     * 本金扣款
     */
    private String realReduce;
    private String consumeBean_cardAccConsumeExt;
    private Helper helper;
    private String score2money;

    public void doConsumeCommon() {
        helper.validate();
        excute();
        //插入消费扩展信息
        cardAccConsumeExtMapper_insertSelective = consumeBean.cardAccConsumeExt;//done
        //更新账户信息（增量）
        cardAccountMapper_updateRecordChange = consumeBean_cardAccountsChange;//done
        //批量插入流水各账户记录
        cardAccountRecordMapper_insertSelective = consumeBean_accountRecords;//done
        //保存返现相关
        //更新消耗的返现记录
        posConsumeDonateGenerateRecMapper_updateByChange = consumeBean_updateDonateGenerateRecordList;//done
        //保存产生的返现记录
        posConsumeDonateGenerateRecMapper_insert = consumeBean_insertDonateGenerateRecordList;//done
        //保存消费的返现明细记录表
        posConsumeDonateCostRecMapper_insert = consumeBean_consumeDonateCostRecordList;//done
        //批量插入消费关联券标
        posConsumptionCouponExtMapper_batchInsert = helper.consumeCounponExtList;//done
        //如果存在券消费，批量插入消费券扩展表
        posConsumeCouponRelItemMapper_batchInsert = helper.consumeCouponRelItemList;//done

    }


    /**
     * 计算扣款金额的本金和赠送的扣款
     */
    private void calcConsumeMoney() {
        realReduce = input;
        //处理消费返现扣减
        dealConsumeDonateReduce();
        //12.验证积分扣减
        score2money = input;
    }

    private void validateUseCoupons() {
    }

    private void validateScoreReduce() {
    }

    /**
     * 处理消费返现扣减
     */
    private void dealConsumeDonateReduce() {
        consumeBean_updateDonateGenerateRecordList = input;
        consumeBean_consumeDonateCostRecordList = input;
    }

    /**
     * 处理消费返现账户
     */
    public void dealConsumeDonateAccount() {
        calcConsumeDonateMoney();
        consumeBean_accountRecords = consumeDonateAccount;
        consumeBean_cardAccountsChange = input;
        posConsumeRelCompanyCfgMapper_selectByCompanyId = "bm_consume_rel_company_cfg";
        consumeBean_insertDonateGenerateRecordList = input;

    }

    /**
     * 计算消费返现金额
     */
    private void calcConsumeDonateMoney() {
        String temp = posConsumeRule;
        consumeDonateMoney = calcDonateMoney;

    }
    private void validateItemCoupon() {
        consumeCounponExtList = input;
        consumeCouponRelItemList = input;
    }

    public void validate() {
        validateCashCoupon();
        validateDiscountCoupon();
        validateItemCoupon();
        validateGiftCoupon();

        // init();//处理5个帐户
        //11.计算本金、赠送、消费返现的扣款
        calcConsumeMoney();

        //12.验证积分扣减
        validateScoreReduce();
        //14.验证优惠券的使用是否合法
        validateUseCoupons();
        //13.验证账单平衡性（保证在最后，在中间过程会出现各个抵用值有问题而归0 的情况）
    }



    public void excute() {
        //获取消费规则
        getConsumeRule();
        //处理本金账户扣减
        dealRealAccount();
        //处理赠送账户
        dealDonateAccount();
        //处理消费返现账户
        dealConsumeDonateAccount();
        //处理积分账户
        dealScoreAcount();
        //产生流水扩展信息
        getPosBmTsExt();
    }

    private void getPosBmTsExt() {
        consumeBean.cardAccConsumeExt = input;
    }

    //处理本金账户扣减
    private void dealRealAccount() {
        consumeBean_accountRecords = realAccount;
        consumeBean_cardAccountsChange = realAccount;
    }

    //处理赠送账户
    private void dealDonateAccount() {
        consumeBean_accountRecords = freeAccount;
        consumeBean_cardAccountsChange = freeAccount;
    }

    //处理积分账户
    private void dealScoreAcount() {
        calcScoreMoney = input;
        donatedScore = calcScoreMoney;
        scoreAccount = input;
        consumeBean_accountRecords = scoreAccount;
        consumeBean_cardAccountsChange = scoreAccount;
    }

    private void getConsumeRule() {
        posConsumeRule = posConsumeRuleMapper_selectByParams;
        posConsumeRule_relItems = posConsumeRuleMapper_selectRelItemsByRuleId;
    }
    private void validateCashCoupon() {
        consumeCounponExtList = input;
    }

    private void validateDiscountCoupon() {
        consumeCounponExtList = input;
    }

    private void validateGiftCoupon() {
        consumeCounponExtList = input;
        consumeCouponRelItemList = input;
    }
}
